﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using IHISCETeim.Model;
using IHISCETeim.BLL;
using IHISCETeim.Library;
using FineUI;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using System.Data;

namespace IHISCETeim.Web.AgentSys.SysMgr
{
    public partial class AgentUserAdd : AgentPageBase
    {


        protected override Permission[] PowerKey
        {
            get
            {
                return new Permission[] { Permission.新增 };
            }
        }
        protected override string DWZRef
        {
            get { return "AgentUserList"; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }
        private void LoadData()
        {
            btnClose.OnClientClick = ActiveWindow.GetConfirmHideReference();
         
            //// 初始化用户所属部门　经销商
            InitUserDept();
           InitRoles();
        }
        #region InitUserRole

        private void InitUserDept()
        {
            // 打开编辑窗口
            string selectDeptURL = String.Format("./SelectDept.aspx?ids=<script>{0}</script>", hfSelectedDept.GetValueReference());
            tbSelectedDept.OnClientTriggerClick = Window1.GetSaveStateReference(hfSelectedDept.ClientID, tbSelectedDept.ClientID,tbxUserName.ClientID)
                    + Window1.GetShowReference(selectDeptURL, "选择经销商");

        }

        private void InitRoles()
        {
            this.ddlRoleId.DataSource=  ORoleController.GetAll().Select("IsAgent=1").ToList();
            this.ddlRoleId.DataBind();

           // List<AgentPowerConfigInfo> list = AgentConfigController.GetAllPowerConfig();
           // list.Add(new AgentPowerConfigInfo() { ID = 0, RealName = "空" });


            List<AgentPowerConfigInfo> list = new List<AgentPowerConfigInfo>();
            GetPowerList(list, 0);
            this.ddlSrvActUserId.DataSource = list;
            this.ddlSrvActUserId.DataBind();


           // this.ddlSrvActUserId.DataSource = OUserController.GetUserByDepartment("'售后服务部'");
           // this.ddlSrvActUserId.DataBind();
        }
       
        #endregion

      

       

        #region Events

        
        private bool SaveItem()
        {            
            OUserInfo objUser = new OUserInfo();
            objUser.UserName = tbxUserName.Text.Trim();
            objUser.PassWord = tbxPassword.Text.Trim();
            objUser.RealName = tbSelectedDept.Text.Trim();
            objUser.UserType = 2;
            objUser.RoleId = Convert.ToInt32( ddlRoleId.SelectedValue);
            objUser.State = 1;
            objUser.CreateDate = DateTime.Now;
            objUser.Mobile = tbxMobile.Text.Trim();
            objUser.EMail = tbxEmail.Text.Trim();
           // objUser.PartsEmail = tbxPartsEmail.Text.Trim();
            objUser.Department = hfSelectedDept.Text.Trim();
            objUser.SrvActUserId = Convert.ToInt32(ddlSrvActUserId.SelectedValue);
            objUser.Remark = this.tbxRemark.Text.Trim();
            return OUserController.Add(objUser);
        }

        protected void btnSaveClose_Click(object sender, EventArgs e)
        {
            string Department  = hfSelectedDept.Text.Trim();
            string userName = this.tbxUserName.Text.Trim();

            if (OUserController.GetByUserName(userName).UserId > 0)
            {
                Alert.Show("登陆名 " + userName + " 已经存在！");
                return;
            }
            //
            DataTable dt = OUserController.GetUserByDepartment(string.Format("'{0}'", Department));
            int num = dt.Select("RoleId=" + ddlRoleId.SelectedValue).Count();
            //
            if (num > 0)
            {
                Alert.Show("经销商 " + Department + " 已经已在系统有帐号,一个经销商只能有一个帐号！");
                return;
            }
          

            if (SaveItem())
            {
                PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
            }
            else {
                Alert.Show("新增经销商用户失败！","提示",MessageBoxIcon.Error);
            }
        }
        #endregion
    }
}